package com.kingint.web.util.thirdPartyInterface;

import com.alibaba.fastjson.JSONObject;
import com.kingint.web.util.spring.Variable;
import org.springframework.stereotype.Component;


@Component
public class DuerosUtil {

    /**
     * 授权接口
     * @return
     */
    public static String login(){
        String url = "https://dueros.baidu.com/business/oauth/v2/token?grant_type=client_credentials&app_id=%s&app_secret=%s";
//       url = String.format(url, "srHEXwQb2xvsPE48NwO8Mrs4NM02NEK7", "RWZ7n7UDsPgYvWlPp2rQ2IGhcVGpbK3u");
        url = String.format(url, Variable.APPID , Variable.APPSECRET);
        String string = JSONObject.parseObject(HttpUtils.HttpsGet(url)).getJSONObject("data").getString("access_token");
        JSONObject jsonObject = new JSONObject();
        if(string != null){
            jsonObject.put("Authorization","Bearer "+string);
        }else{
            return "null";
        }
        return jsonObject.toJSONString();
    }

    /**
     * 获取小度在线情况
     * @param cid cuid设备id
     * @param shopId 酒店编号
     * @param shopRoomNum 酒店房号
     * @return
     */
    public static String duerOnline(String cid ,String shopId ,String shopRoomNum){
        String url = "https://dueros.baidu.com/business/open/rest/robot";
        JSONObject jsonObject = new JSONObject(true);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("method","getDeviceStatus");
        if(cid != null){
            jsonObject.put("cuid",cid);
            jsonObject1.put("cuid",cid);
            url = "https://dueros.baidu.com/business/open/restful";
        }else{
            jsonObject.put("shopId",shopId);
            jsonObject.put("shopRoomNum",shopRoomNum);
        }
        jsonObject.put("timestamp",System.currentTimeMillis() / 1000+"");
        jsonObject.put("v","2.0");
        jsonObject.put("source","kt");
        jsonObject.put("paramJson",jsonObject1.toJSONString());
        return HttpUtils.HttpsPost(url, jsonObject.toJSONString(),login());
    }
    public static String duerOnline(String cid){
        return duerOnline(cid,null,null);
    }
    public static String duerOnline(String shopId ,String shopRoomNum){
        return duerOnline(null,shopId,shopRoomNum);
    }

    /**
     * 获取房间对应的小度id
     * @return
     */
    public static String getRoomCuid(String shopId){
        String url = "https://dueros.baidu.com/business/open/rest/shopinfo";
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.put("shopId",shopId);
        jsonObject.put("timestamp",System.currentTimeMillis() / 1000+"");
        jsonObject.put("v","2.0");
        jsonObject.put("source","kt");
        jsonObject.put("paramJson","{\"method\":\"deviceRoom\"}");
        return HttpUtils.HttpsPost(url, jsonObject.toJSONString(),login());
    }

    /**
     * 推送欢迎语
     * @param cuid 小度id/或者酒店id
     * @param shopRoomNum 房间号（仅在选择酒店id时使用）
     * @param Text 语音欢迎语
     * @param music 背景音乐地址
     * @return
     */
    public static String pushWelcome(String cuid ,String shopRoomNum ,String Text ,String music){
        String url = "https://dueros.baidu.com/business/open/restful";
        JSONObject jsonObject = new JSONObject(true);
        if(shopRoomNum != null){
            url="https://dueros.baidu.com/business/open/rest/robot";
            jsonObject.put("shopId",cuid);
            jsonObject.put("shopRoomNum",shopRoomNum);
        }else {
            jsonObject.put("cuid",cuid);
        }
        jsonObject.put("timestamp",System.currentTimeMillis() / 1000+"");
        jsonObject.put("v","2.0");
        jsonObject.put("source","kt");
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("method","welcome");
        jsonObject1.put("outputSpeechText",Text);
        jsonObject1.put("cardContent",Text);
        jsonObject1.put("cuid",cuid);
        if(music != null){
            jsonObject1.put("backgroundMusic",music);
        }
        jsonObject.put("paramJson",jsonObject1.toString());
        return HttpUtils.HttpsPost(url, jsonObject.toJSONString(),login());
    }
    public static String pushWelcome(String cuid ,String Text){
        return pushWelcome(cuid,null,Text,null);
    }
    public static String pushWelcome(String shopId, String shopRoomNum ,String Text){
        return pushWelcome(shopId,shopRoomNum,Text,null);
    }


    /**
     * 回调客需订单情况
     * @param cuid 小度id/或者酒店id
     * @param messageId 消息id
     * @param status 状态1：已完成 2：处理异常
     * @param reason 当statue为2时必填
     * @return
     */
    public static String callBack(String cuid ,String messageId ,String status ,String reason){
        String url = "https://dueros.baidu.com/business/open/restful";
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.put("cuid",cuid);
        jsonObject.put("timestamp",System.currentTimeMillis() / 1000+"");
        jsonObject.put("v","2.0");
        jsonObject.put("source","kt");
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("method","callback");
        jsonObject1.put("messageId",messageId);
        jsonObject1.put("cuid",cuid);
        jsonObject1.put("status",status);
        if(status.equals("2")){
            jsonObject1.put("reason",reason);
        }
        jsonObject.put("paramJson",jsonObject1.toString());
        return HttpUtils.HttpsPost(url, jsonObject.toJSONString(),login());
    }


    public static void main(String[] args) {
        String cuid = "8K19521DF05A354E";
        String text = "大家好，我嘿渣渣灰，是朋友就来贪玩蓝月，顶花带刺水嫩小黄瓜，白了透绿的大白菜";
      //  System.out.println(pushWelcome("2920","6001",text));
      //  System.out.println(getRoomCuid("2920"));
        System.out.println( duerOnline("3d6df925a90b615e82055627826c8cf9"));

//        String roomCuid = getRoomCuid("2920");
//        HashMap hashMap = new Gson().fromJson(roomCuid, HashMap.class);
//        Object data = hashMap.get("data");
//        ArrayList arrayList = new Gson().fromJson(data.toString(), ArrayList.class);
//        getcuid getcuid = new Gson().fromJson(arrayList.get(0).toString(), getcuid.class);
//        System.out.println(getcuid.getCuid()+"------"+getcuid.getRoomName());
//        for (Object o : arrayList) {
//            getcuid getcuid= (getcuid) o;
//            if(getcuid.getRoomName().equals("10111")){
//                System.out.println(getcuid.getCuid());
//            }
//        }

    }


}
